package com.zy.mc_module.ui.main.forget;

import static com.zy.mc_module.mvp.presenter.LoginPresenter.IMG_CODE;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.CountDownTimer;
import android.widget.EditText;
import android.widget.Space;
import android.widget.TextView;

import com.zy.mc_module.R;
import com.zy.mc_module.base.BaseActivity;
import com.zy.mc_module.databinding.DialogCheckImgCodeBinding;
import com.zy.mc_module.ui.main.forget.ForgetContract.View;
import com.zy.mc_module.utils.GlideUtil;
import com.zy.mc_module.utils.NoDoubleUtil;
import com.zy.mc_module.utils.StatusBarUtil;
import com.zy.mc_module.utils.StringsUtils;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.view.CustomHeadLayout;
import com.zy.mc_module.utils.retrofit.error.ThdException;
import com.zy.mc_module.wheel.dialog.CommonDialog;

import java.io.InputStream;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;

/**
 * Created on 19/1/3
 * 忘记密码
 */
public class ForgetActivity extends BaseActivity<View, ForgetPresenter> implements ForgetContract.View {
    @BindView(R.id.statusBar)
    Space statusBar;
    @BindView(R.id.customhead)
    CustomHeadLayout customhead;
    @BindView(R.id.forget_et_tel)
    EditText et_tel;
    @BindView(R.id.forget_et_code)
    EditText et_code;
    @BindView(R.id.forget_tv_getcode)
    TextView tv_getcode;
    @BindView(R.id.forget_et_pwd)
    EditText et_pwd;
    private CommonDialog checkDialog;
    private DialogCheckImgCodeBinding dialogBinding;

    private CountDownTimer count;

    private String code = "", tel = "", pwd = "",imgCode;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget;
    }

    @Override
    protected ForgetPresenter createPresenter() {
        return new ForgetPresenter();
    }

    @Override
    protected void initView() {
        StatusBarUtil.setImmersiveStatusBar(this);
        StatusBarUtil.setStatusHeight(context, statusBar);

        count = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_getcode.setClickable(false);
                tv_getcode.setText(millisUntilFinished / 1000 + " s");
            }

            @Override
            public void onFinish() {
                tv_getcode.setClickable(true);
                tv_getcode.setText("发送验证码");
            }
        };

        dialogBinding = DialogCheckImgCodeBinding.inflate(getLayoutInflater());
        checkDialog = new CommonDialog(context, false, dialogBinding.getRoot());

    }

    @Override
    protected void initListener() {
        customhead.setLeftClickListener(view -> finish());
        setDialogListener();
    }

    private void setDialogListener() {
        dialogBinding.dialogHintSwitch.setOnClickListener(v -> {
            getImgCode();
        });
        dialogBinding.hintCancel.setOnClickListener(v -> checkDialog.dismiss());
        dialogBinding.hintSure.setOnClickListener(v -> {
            String code = dialogBinding.hintContent.getText().toString();
            if (StringsUtils.isEmpty(code)) {
                ToastUtil.show("请输入验证码");
                return;
            }
            imgCode = code;
            presenter.getForgetSmsCode(context, et_tel.getText().toString(), code);
        });
    }

    private void getImgCode() {
        if (NoDoubleUtil.isFastClick())
            presenter.getImgCode(context, tel);
    }

    /***
     * 获取验证码
     */
    @OnClick(R.id.forget_tv_getcode)
    public void code() {
        tel = et_tel.getText().toString().trim();
        if (!StringsUtils.isMobile(tel)) {
            return;
        }
        count.start();
        if (imgCode == null) {
            getImgCode();
        } else {
            presenter.getForgetSmsCode(context, et_tel.getText().toString(), imgCode);
        }
    }

    /***
     * 确认
     */
    @OnClick(R.id.forget_tv_sure)
    public void sure() {
        code = et_code.getText().toString().trim();
        tel = et_tel.getText().toString().trim();
        pwd = et_pwd.getText().toString().trim().toLowerCase();
        if (!StringsUtils.isMobile(tel)) {
            return;
        }
        if (code.isEmpty()) {
            ToastUtil.showShort("验证码不能为空");
            return;
        }
        if (!StringsUtils.checkPwd(pwd)) {
            ToastUtil.showShort("请输入6~20位字母和数字组成的密码");
            return;
        }
        presenter.forget(context, tel, code, pwd);
    }

    @Override
    public void onSuccess() {
        ToastUtil.showShort("重置密码成功，快来登录吧！");
        finish();
    }

    @Override
    public <T> void onSuccess(T t, int type) {
        if (type == 12){
            //发送验证码成功
            ToastUtil.show("验证码已发送");
            if (checkDialog.isShowing())
                checkDialog.dismiss();
        }else if (type == IMG_CODE){
            ResponseBody body = (ResponseBody) t;
            try {
                InputStream byteStream = body.byteStream();
                Bitmap bitmap = BitmapFactory.decodeStream(byteStream);
                GlideUtil.displayImage(dialogBinding.dialogHintImg, bitmap, R.mipmap.uvv_on_error);
                if (!checkDialog.isShowing()) {
                    checkDialog.show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(ThdException error) {
        imgCode = null;
        ToastUtil.showShort(error.message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != count) {
            count.cancel();
        }
    }
}
